'use strict';

var _ = require('lodash');
var mysql = require('mysql');
var DB = require('../../lib/mysqlDB');
var async = require('async');

/**
 * 后台默认首页
 * @param req
 * @param res
 */

module.exports.index = function(req,res){
    var id = req.query.id;
    var result;

    var sql = "select * from ?? order by id DESC";
    var options = ['user'];
    sql = mysql.format(sql,options);

    async.waterfall([
        function(done){
            var sql = "select * from ?? order by id DESC";
            var options = ['user'];
            sql = mysql.format(sql,options);

            DB.select(sql,function(error,result){
                if(!result){
                    result = '';
                }

                done(null,result);
            });
        },
        function(list1,done){
            var sql = "select * from ?? order by id DESC";
            var options = ['user'];
            sql = mysql.format(sql,options);

            var tmp = [];
            if(list1.length > 0){
                list1.map(function(item){
                    tmp.push(item);
                });
            }

            DB.select(sql,function(error,result){
                if(!result){
                    result = [];
                }

                if(result.length > 0){
                    result.map(function(item){
                        tmp.push(item);
                    });
                }
                done(null,tmp);
            });
        }
    ],function(error,result){
        if(!result){
            result = '';
        }
        var templates_data = new Object();
        templates_data['title'] = 'Qeeniao Admin';
        templates_data['result'] = result;

        res.render('admin/index',templates_data);
    });
};